package com.zsrt.cookie;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zsrt.cookie.Utils.CookieUtil;

public class CookieServlet extends HttpServlet {

	
	private static final long serialVersionUID = 5103456890240354012L;

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		//拿到lasttime
		if(CookieUtil.getCookie(request, "lasttime")==null){
			String time=new Date().toString();
			Cookie c1=new Cookie("lasttime",time);
			Cookie c2=new Cookie("currenttime",time);
			response.addCookie(c1);
			response.addCookie(c2);
		}else{
			String time=new Date().toString();
			Cookie c1=new Cookie("lasttime",CookieUtil.getCookie(request, "currenttime").toString());
			Cookie c2=new Cookie("currenttime",time);
			response.addCookie(c1);
			response.addCookie(c2);
		}
		response.sendRedirect(request.getContextPath()+"/cookie.jsp");
		
	}
}
